//该类库由Benny Yang提供


class DateTime {
    public JsDate: Date;

    get Now() {
        let dt = new DateTime();
        return dt;
    }

    constructor(jsDate: Date = new Date()) {
        this.JsDate = jsDate;
    }

    //格式化方法， 用法与C#的API一样
    ToString(fmt: string) {
        var o = {
            "M+": this.JsDate.getMonth() + 1,                 //月份   
            "d+": this.JsDate.getDate(),                    //日   
            "H+": this.JsDate.getHours(),                   //小时   
            "m+": this.JsDate.getMinutes(),                 //分  
            "s+": this.JsDate.getSeconds(),                 //秒   
            "q+": Math.floor((this.JsDate.getMonth() + 3) / 3), //季度   
            "f+": this.JsDate.getMilliseconds()             //毫秒   
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.JsDate.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                let pos = ("00" + o[k]).length - RegExp.$1.length;
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(pos)));
            }
        }
        return fmt;
    }

    //字符串转DateTime
    static Parse(str: string) {
        str = str.replace(/\d+(?=-[^-]+$)/, function (a) { return (parseInt(a, 10) ).toString(); });
        let r = str.match(/\d+/g);
        var date = new Date(r);

        var dt = new DateTime(date);
        if (str.indexOf('PM') > 0)
            dt = dt.AddHours(12);
        return dt; 
    }

    AddHours(hours: number) {
        var val =  this.JsDate ;
        var date = new Date(val.setHours(val.getHours() + hours));
        return new DateTime(date);
    }

}

